package net.maku.express.biza.controller.orderlist;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import net.maku.express.biza.controller.orderlist.vo.*;
import net.maku.express.biza.service.orderlist.OrderListService;
import net.maku.framework.common.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author wenyignying'
 * @Description:订单列表
 * @Date
 */
@RequestMapping("orderList")
@RestController
public class OrderListController {
    @Autowired
    private OrderListService orderListService;

    @PostMapping("/add")
    @Operation(summary = "添加")
    @PreAuthorize("hasAuthority('express:package:add')")
    public Result<Integer> add(@RequestBody @Valid OrderListVO orderListVO) {
        return orderListService.add(orderListVO);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('express:package:delete')")
    public Result<Integer> delete(@Valid Long id) {
        return orderListService.delete(id);
    }


    @PutMapping("/update")
    @Operation(summary = "更新")
    @PreAuthorize("hasAuthority('express:package:update')")
    public Result<Integer> update(@RequestBody @Valid OrderListUpdateRequestVO orderListUpdateRequestVO) {
        return orderListService.update(orderListUpdateRequestVO);
    }

    @PostMapping("/select")
    @Operation(summary = "查询")
    @PreAuthorize("hasAuthority('express:package:select')")
    public Result<Page<OrderListSelectResponseVO>> select(@RequestBody @Valid OrderListSelectRequestVO orderListSelectRequestVO) {
        return orderListService.select(orderListSelectRequestVO);
    }

    @PostMapping("/selectOne")
    @Operation(summary = "查询单条数据")
    @PreAuthorize("hasAuthority('express:package:select')")
    public Result<OrderListSelectOneResponseVO> select(@RequestBody @Valid JSONObject jsonObject) {
        if (!jsonObject.containsKey("id")) {
            return Result.error("缺少id参数!");
        }
        Long id = jsonObject.getLong("id");
        return orderListService.selectOne(id);
    }

    @PostMapping("/export")
    @Operation(summary = "导出")
    @PreAuthorize("hasAuthority('express:package:select')")
    public void export(HttpServletResponse response) throws IOException {
        response.addHeader("Cache-Control", "no-cache");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.addDateHeader("Expires", 0);
        response.setCharacterEncoding("UTF-8");
        ArrayList<Object> objects = new ArrayList<>();
        List<OrderPackingListVO> e = new ArrayList<OrderPackingListVO>() {};

        objects.add(e);
        //去除合并单元格的处理    2024.07.11
//        EasyExcel.write(response.getOutputStream())
//                .head(OrderPackingListVO.class)
//                .sheet()
//                .registerWriteHandler(new MergeCellStrategyHandler(true, true, new HashSet<Integer>() {{
//                    add(e.size());
//                }}, new HashSet<Integer>() {{
//                    this.add(0);
//                    this.add(1);
//                    this.add(2);
//                    this.add(7);
//                }}))
//                .doWrite(objects);

        EasyExcel.write(response.getOutputStream())
                .head(OrderPackingListVO.class)
                .sheet()
                .doWrite(objects);

    }
}
